package com.corticon.dialogs.model;

import com.corticon.dialogs.util.QuestionUtils;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;


/**
 * QuestionInSet entity.
 * 
 * @author Lifan Yang
 */
@Entity
@Table(name = "q_page_question", uniqueConstraints = {@UniqueConstraint(columnNames = {
        "questionassoc_question_id_oid", "pageassoc_q_page_id_oid"})})
public class QuestionInSet extends AuditableEntity implements Serializable, Comparable {

    //    private QuestionInSetId id = new QuestionInSetId();
    private Long id;
    private Question question;
    private QuestionSet questionSet;
    private String sequence;
    private Double priority;

    public QuestionInSet() {
    }

    public QuestionInSet(Question question, QuestionSet questionSet, String sequence,
                         double priority) {
        this.question = question;
        this.questionSet = questionSet;
        this.sequence = sequence;
        this.priority = priority;
    }

    @Id
    @SequenceGenerator(name = "q_page_question_id_seq",
            sequenceName = "q_page_question_id_seq",
            allocationSize = 1, initialValue = 1)
    // Set allocationSize and initialValue
    // to let sequence increment following DB.
    @GeneratedValue(strategy = GenerationType.AUTO, generator =
            "q_page_question_id_seq")
    @Column(name = "q_page_question_id",  nullable = false, columnDefinition = "integer")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name = "questionassoc_question_id_oid")
    public Question getQuestion() {
        return this.question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "pageassoc_q_page_id_oid")
    public QuestionSet getQuestionSet() {
        return this.questionSet;
    }

    public void setQuestionSet(QuestionSet questionSet) {
        this.questionSet = questionSet;
    }

    /**
     * SEQUENCE is reserved keyword of SQL-99.
     */
    //    @Column(name = "_SEQUENCE")
    @Column(name = "sequence")
    public String getSequence() {
        return this.sequence;
    }

    public void setSequence(String sequence) {
        this.sequence = sequence;
    }

    @Column(name = "priority")
    public double getPriority() {
        return priority;
    }

    public void setPriority(double priority) {
        this.priority = priority;
    }

    // ********** Helper Methods ********** //
    public void remove() {
        this.question.getQuestionInSets().remove(this);
        this.questionSet.getQuestionsInSet().remove(this);
    }

    public int compareTo(Object questionInSet) {
        return QuestionUtils.compareSequence(this.getSequence(), ((QuestionInSet) questionInSet).getSequence());
    }
}